import { RouterMount, createRouter } from 'uni-simple-router';
import store from '@/store/index'
import { app } from '@/main'

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [...ROUTES]
});
//全局路由前置守卫
router.beforeEach(async (to, from, next) => {
  const { path } = to
  const { token, userInfo, userId } = store.getters
  // 用户信息
  if (token && !userInfo) {
    const res = await app.$u.api.getUserById({ id: userId })
    store.commit('user/SET_USER_INFO', res[0]);
  }
  const blackList = ['order', 'user']
  // 登录后不能进 login
  if (path.includes('login') && token) return next('/pages/mine/mine')
  // 未登录不能进 order + user
  // 进入 login 记录页面
  if (blackList.some(item => path.includes(item)) && !token) return next({ path: `/pages/login/login?form=${path}` })
  next();
});
// 全局路由后置守卫
/* router.afterEach((to, from) => {
  console.log('跳转结束')
}) */

export {
  router,
  RouterMount
}